﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls;
using Miris.HumanMilkAnalyser.DataModel;
using Miris.HumanMilkAnalyser.Service.Validation;

namespace Miris.HumanMilkAnalyser.WPF.ValidationRules
{
    public class PatientNumberValidationRule : ValidationRule
    {
        public override ValidationResult Validate(object value, System.Globalization.CultureInfo cultureInfo)
        {
            var patientlValidator = new PatientValidator();

            var patient = new Patient
            {
                Number = value.ToString().Trim()
            };

            if (!patientlValidator.CheckRequiredNumber(patient))
            {
                return new ValidationResult(false, patientlValidator.GetError(PatientValidator.NumberErrorKey));
            }
            else if (!patientlValidator.CheckNumberLength(patient))
            {
                return new ValidationResult(false, patientlValidator.GetError(PatientValidator.NumberErrorKey));
            }

            return new ValidationResult(true, null);
        }
    }

    public class MotherNameValidationRule : ValidationRule
    {
        public override ValidationResult Validate(object value, System.Globalization.CultureInfo cultureInfo)
        {
            var patientlValidator = new PatientValidator();

            var patient = new Patient
            {
                MotherName = value.ToString().Trim()
            };

            if (!patientlValidator.CheckRequiredMotherName(patient))
            {
                return new ValidationResult(false, patientlValidator.GetError(PatientValidator.MotherNameErrorKey));
            }
            else if (!patientlValidator.CheckMotherNameLength(patient))
            {
                return new ValidationResult(false, patientlValidator.GetError(PatientValidator.MotherNameErrorKey));
            }

            return new ValidationResult(true, null);
        }
    }

    public class MotherAgeValidationRule : ValidationRule
    {
        public override ValidationResult Validate(object value, System.Globalization.CultureInfo cultureInfo)
        {
            var patientlValidator = new PatientValidator();
            var age = value as string;
            short result;
            var convertSuccessful = short.TryParse(age, out result);

            if (!string.IsNullOrEmpty(age) && !convertSuccessful)
            {
                return new ValidationResult(false, "年龄只能为数字");
            }
            else if (convertSuccessful)
            {
                var patient = new Patient
                {
                    MotherAge = result
                };

                if (!patientlValidator.CheckMotherAge(patient))
                {
                    return new ValidationResult(false, patientlValidator.GetError(PatientValidator.MotherAgeErrorKey));
                }
            }

            return new ValidationResult(true, null);
        }
    }

    public class MotherHeightValidationRule : ValidationRule
    {
        public override ValidationResult Validate(object value, System.Globalization.CultureInfo cultureInfo)
        {
            var patientlValidator = new PatientValidator();
            var height = value as string;
            double result;
            var convertSuccessful = double.TryParse(height, out result);

            if (!string.IsNullOrEmpty(height) && !convertSuccessful)
            {
                return new ValidationResult(false, "身高只能为数字");
            }
            else if (convertSuccessful)
            {
                var patient = new Patient
                {
                    MotherHeight = result
                };

                if (!patientlValidator.CheckMotherHeight(patient))
                {
                    return new ValidationResult(false, patientlValidator.GetError(PatientValidator.MotherHeightErrorKey));
                }
            }

            return new ValidationResult(true, null);
        }
    }

    public class MotherWeightValidationRule : ValidationRule
    {
        public override ValidationResult Validate(object value, System.Globalization.CultureInfo cultureInfo)
        {
            var patientlValidator = new PatientValidator();
            var weight = value as string;
            double result;
            var convertSuccessful = double.TryParse(weight, out result);

            if (!string.IsNullOrEmpty(weight) && !convertSuccessful)
            {
                return new ValidationResult(false, "体重只能为数字");
            }
            else if (convertSuccessful)
            {
                var patient = new Patient
                {
                    MotherWeight = result
                };

                if (!patientlValidator.CheckMotherWeight(patient))
                {
                    return new ValidationResult(false, patientlValidator.GetError(PatientValidator.MotherWeightErrorKey));
                }
            }

            return new ValidationResult(true, null);
        }
    }

    public class MotherEmotionValidationRule : ValidationRule
    {
        public override ValidationResult Validate(object value, System.Globalization.CultureInfo cultureInfo)
        {
            var patientlValidator = new PatientValidator();

            var patient = new Patient
            {
                MotherEmotion = value.ToString().Trim()
            };

            if (!patientlValidator.CheckMotherEmotionLength(patient))
            {
                return new ValidationResult(false, patientlValidator.GetError(PatientValidator.MotherEmotionErrorKey));
            }

            return new ValidationResult(true, null);
        }
    }

    public class MotherBadHabitValidationRule : ValidationRule
    {
        public override ValidationResult Validate(object value, System.Globalization.CultureInfo cultureInfo)
        {
            var patientlValidator = new PatientValidator();

            var patient = new Patient
            {
                MotherBadHabit = value.ToString().Trim()
            };

            if (!patientlValidator.CheckMotherBadHabitLength(patient))
            {
                return new ValidationResult(false, patientlValidator.GetError(PatientValidator.MotherBadHabitErrorKey));
            }

            return new ValidationResult(true, null);
        }
    }

    public class BabyNameValidationRule : ValidationRule
    {
        public override ValidationResult Validate(object value, System.Globalization.CultureInfo cultureInfo)
        {
            var patientlValidator = new PatientValidator();

            var patient = new Patient
            {
                BabyName = value.ToString().Trim()
            };

            if (!patientlValidator.CheckRequiredBabyName(patient))
            {
                return new ValidationResult(false, patientlValidator.GetError(PatientValidator.BabyNameErrorKey));
            }
            else if (!patientlValidator.CheckBabyNameLength(patient))
            {
                return new ValidationResult(false, patientlValidator.GetError(PatientValidator.BabyNameErrorKey));
            }

            return new ValidationResult(true, null);
        }
    }

    public class BabyHeightValidationRule : ValidationRule
    {
        public override ValidationResult Validate(object value, System.Globalization.CultureInfo cultureInfo)
        {
            var patientlValidator = new PatientValidator();
            var height = value as string;
            double result;
            var convertSuccessful = double.TryParse(height, out result);

            if (!string.IsNullOrEmpty(height) && !convertSuccessful)
            {
                return new ValidationResult(false, "身高只能为数字");
            }
            else if (convertSuccessful)
            {
                var patient = new Patient
                {
                    BabyHeight = result
                };

                if (!patientlValidator.CheckBabyHeight(patient))
                {
                    return new ValidationResult(false, patientlValidator.GetError(PatientValidator.BabyHeightErrorKey));
                }
            }

            return new ValidationResult(true, null);
        }
    }

    public class BabyWeightValidationRule : ValidationRule
    {
        public override ValidationResult Validate(object value, System.Globalization.CultureInfo cultureInfo)
        {
            var patientlValidator = new PatientValidator();
            var weight = value as string;
            double result;
            var convertSuccessful = double.TryParse(weight, out result);

            if (!string.IsNullOrEmpty(weight) && !convertSuccessful)
            {
                return new ValidationResult(false, "体重只能为数字");
            }
            else if (convertSuccessful)
            {
                var patient = new Patient
                {
                    BabyWeight = result
                };

                if (!patientlValidator.CheckBabyWeight(patient))
                {
                    return new ValidationResult(false, patientlValidator.GetError(PatientValidator.BabyWeightErrorKey));
                }
            }

            return new ValidationResult(true, null);
        }
    }

    public class BabyHeadCircumferenceValidationRule : ValidationRule
    {
        public override ValidationResult Validate(object value, System.Globalization.CultureInfo cultureInfo)
        {
            var patientlValidator = new PatientValidator();
            var headCircumference = value as string;
            double result;
            var convertSuccessful = double.TryParse(headCircumference, out result);

            if (!string.IsNullOrEmpty(headCircumference) && !convertSuccessful)
            {
                return new ValidationResult(false, "头围只能为数字");
            }
            else if (convertSuccessful)
            {
                var patient = new Patient
                {
                    BabyHeadCircumference = result
                };

                if (!patientlValidator.CheckBabyHeadCircumference(patient))
                {
                    return new ValidationResult(false, patientlValidator.GetError(PatientValidator.BabyHeadCircumferenceErrorKey));
                }
            }

            return new ValidationResult(true, null);
        }
    }

    public class BabyBirthHeightValidationRule : ValidationRule
    {
        public override ValidationResult Validate(object value, System.Globalization.CultureInfo cultureInfo)
        {
            var patientlValidator = new PatientValidator();
            var height = value as string;
            double result;
            var convertSuccessful = double.TryParse(height, out result);

            if (convertSuccessful)
            {
                var patient = new Patient
                {
                    BabyBirthHeight = result
                };

                if (!patientlValidator.CheckBabyBirthHeight(patient))
                {
                    return new ValidationResult(false, patientlValidator.GetError(PatientValidator.BabyBirthHeightErrorKey));
                }
            }

            return new ValidationResult(true, null);
        }
    }

    public class BabyBirthWeightValidationRule : ValidationRule
    {
        public override ValidationResult Validate(object value, System.Globalization.CultureInfo cultureInfo)
        {
            var patientlValidator = new PatientValidator();
            var weight = value as string;
            double result;
            var convertSuccessful = double.TryParse(weight, out result);

            if (convertSuccessful)
            {
                var patient = new Patient
                {
                    BabyBirthWeight = result
                };

                if (!patientlValidator.CheckBabyBirthWeight(patient))
                {
                    return new ValidationResult(false, patientlValidator.GetError(PatientValidator.BabyBirthWeightErrorKey));
                }
            }

            return new ValidationResult(true, null);
        }
    }

    public class BabyBirthHeadCircumferenceValidationRule : ValidationRule
    {
        public override ValidationResult Validate(object value, System.Globalization.CultureInfo cultureInfo)
        {
            var patientlValidator = new PatientValidator();
            var headCircumference = value as string;
            double result;
            var convertSuccessful = double.TryParse(headCircumference, out result);

            if (convertSuccessful)
            {
                var patient = new Patient
                {
                    BabyBirthHeadCircumference = result
                };

                if (!patientlValidator.CheckBabyBirthHeadCircumference(patient))
                {
                    return new ValidationResult(false, patientlValidator.GetError(PatientValidator.BabyBirthHeadCircumferenceErrorKey));
                }
            }

            return new ValidationResult(true, null);
        }
    }
}
